const { sequelize } = require('../config/database');
const User = require('../models/User');
const StudyProgress = require('../models/StudyProgress');
const CheckIn = require('../models/CheckIn');
const Resource = require('../models/Resource');

const resetDatabase = async () => {
  try {
    console.log('🔄 开始重置数据库...');
    
    // 强制重新创建所有表
    await sequelize.sync({ force: true });
    console.log('✅ 数据库表重置成功');
    
    // 创建默认管理员用户
    const adminUser = await User.create({
      username: 'admin',
      email: 'admin@study.com',
      password: 'admin123456',
      real_name: '系统管理员',
      role: 'admin'
    });
    console.log('✅ 创建默认管理员用户成功');
    
    // 完整的54天学习计划数据
    const studyPlan = [
      // 第1阶段：Web基础 (14天)
      { stage: 1, day: 1, title: 'HTML基础语法与标签' },
      { stage: 1, day: 2, title: 'HTML表单与表格' },
      { stage: 1, day: 3, title: 'CSS基础选择器与样式' },
      { stage: 1, day: 4, title: 'CSS盒模型与布局' },
      { stage: 1, day: 5, title: 'CSS Flexbox布局' },
      { stage: 1, day: 6, title: 'CSS Grid布局' },
      { stage: 1, day: 7, title: 'CSS响应式设计' },
      { stage: 1, day: 8, title: 'JavaScript基础语法' },
      { stage: 1, day: 9, title: 'JavaScript函数与作用域' },
      { stage: 1, day: 10, title: 'JavaScript DOM操作' },
      { stage: 1, day: 11, title: 'JavaScript事件处理' },
      { stage: 1, day: 12, title: 'JavaScript Ajax与Fetch' },
      { stage: 1, day: 13, title: 'ES6+新特性' },
      { stage: 1, day: 14, title: 'Web基础综合项目' },
      
      // 第2阶段：C#编程 (16天)
      { stage: 2, day: 15, title: 'C#基础语法与数据类型' },
      { stage: 2, day: 16, title: 'C#面向对象编程' },
      { stage: 2, day: 17, title: 'C#类与对象' },
      { stage: 2, day: 18, title: 'C#继承与多态' },
      { stage: 2, day: 19, title: 'C#接口与抽象类' },
      { stage: 2, day: 20, title: 'C#异常处理' },
      { stage: 2, day: 21, title: 'C#集合与泛型' },
      { stage: 2, day: 22, title: 'C#文件操作与IO' },
      { stage: 2, day: 23, title: 'C#多线程编程' },
      { stage: 2, day: 24, title: 'C#数据库操作' },
      { stage: 2, day: 25, title: 'C#网络编程' },
      { stage: 2, day: 26, title: 'C#Windows窗体应用' },
      { stage: 2, day: 27, title: 'C#WPF基础' },
      { stage: 2, day: 28, title: 'C#ASP.NET Core基础' },
      { stage: 2, day: 29, title: 'C#实用工具开发' },
      { stage: 2, day: 30, title: 'C#综合项目实战' },
      
      // 第3阶段：Vue.js (12天)
      { stage: 3, day: 31, title: 'Vue.js基础与安装' },
      { stage: 3, day: 32, title: 'Vue.js模板语法' },
      { stage: 3, day: 33, title: 'Vue.js组件化开发' },
      { stage: 3, day: 34, title: 'Vue.js生命周期' },
      { stage: 3, day: 35, title: 'Vue.js数据绑定与事件' },
      { stage: 3, day: 36, title: 'Vue.js计算属性与侦听器' },
      { stage: 3, day: 37, title: 'Vue.js组件通信' },
      { stage: 3, day: 38, title: 'Vue.js路由管理' },
      { stage: 3, day: 39, title: 'Vue.js状态管理(Pinia)' },
      { stage: 3, day: 40, title: 'Vue.js HTTP请求与API' },
      { stage: 3, day: 41, title: 'Vue.js项目构建与部署' },
      { stage: 3, day: 42, title: 'Vue.js综合项目实战' },
      
      // 第4阶段：CEF3浏览器自动化 (12天)
      { stage: 4, day: 43, title: 'CEF3框架介绍与环境搭建' },
      { stage: 4, day: 44, title: 'CEF3基础配置与初始化' },
      { stage: 4, day: 45, title: 'CEF3浏览器控件集成' },
      { stage: 4, day: 46, title: 'CEF3 JavaScript交互' },
      { stage: 4, day: 47, title: 'CEF3页面导航与事件处理' },
      { stage: 4, day: 48, title: 'CEF3资源拦截与处理' },
      { stage: 4, day: 49, title: 'CEF3自定义协议与方案' },
      { stage: 4, day: 50, title: 'CEF3多进程架构' },
      { stage: 4, day: 51, title: 'CEF3性能优化' },
      { stage: 4, day: 52, title: 'CEF3实际应用场景' },
      { stage: 4, day: 53, title: 'CEF3与现代Web技术整合' },
      { stage: 4, day: 54, title: 'CEF3项目实战与总结' }
    ];
    
    // 初始化学习进度数据
    for (const plan of studyPlan) {
      await StudyProgress.create({
        user_id: adminUser.id,
        day_number: plan.day,
        stage: plan.stage,
        title: plan.title,
        is_completed: false,
        study_time_minutes: 0,
        homework_status: 'not_started'
      });
    }
    console.log('✅ 初始化54天学习进度数据成功');
    
    // 添加一些示例资源
    const sampleResources = [
      {
        title: 'HTML5教程',
        description: 'HTML5基础教程，适合初学者',
        url: 'https://www.w3school.com.cn/html5/',
        category: 'HTML',
        type: 'course',
        difficulty: 'beginner',
        created_by: adminUser.id
      },
      {
        title: 'CSS3参考手册',
        description: 'CSS3完整参考手册',
        url: 'https://www.w3school.com.cn/css3/',
        category: 'CSS',
        type: 'document',
        difficulty: 'intermediate',
        created_by: adminUser.id
      },
      {
        title: 'JavaScript教程',
        description: 'JavaScript入门到精通',
        url: 'https://www.w3school.com.cn/js/',
        category: 'JavaScript',
        type: 'course',
        difficulty: 'beginner',
        created_by: adminUser.id
      },
      {
        title: 'Vue.js官方文档',
        description: 'Vue.js3.x官方文档',
        url: 'https://cn.vuejs.org/',
        category: 'Vue',
        type: 'document',
        difficulty: 'intermediate',
        created_by: adminUser.id
      },
      {
        title: 'C#编程指南',
        description: 'Microsoft C#编程指南',
        url: 'https://docs.microsoft.com/zh-cn/dotnet/csharp/',
        category: 'C#',
        type: 'document',
        difficulty: 'intermediate',
        created_by: adminUser.id
      },
      {
        title: 'CEF3开发指南',
        description: 'Chromium Embedded Framework开发文档',
        url: 'https://github.com/chromiumembedded/cef',
        category: 'CEF3',
        type: 'document',
        difficulty: 'advanced',
        created_by: adminUser.id
      }
    ];
    
    for (const resource of sampleResources) {
      await Resource.create(resource);
    }
    console.log('✅ 创建示例资源成功');
    
    console.log('🎉 数据库重置完成！');
    console.log('📊 已创建54天完整学习计划：');
    console.log('   第1阶段：Web基础 (第1-14天)');
    console.log('   第2阶段：C#编程 (第15-30天)');
    console.log('   第3阶段：Vue.js (第31-42天)');
    console.log('   第4阶段：CEF3实战 (第43-54天)');
    console.log('');
    console.log('📝 默认管理员账户:');
    console.log('   用户名: admin');
    console.log('   密码: admin123456');
    
  } catch (error) {
    console.error('❌ 重置数据库失败:', error);
  } finally {
    await sequelize.close();
    process.exit(0);
  }
};

resetDatabase(); 